﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Entity;
using BO;

namespace Final_Prototype_UsingDev_QuanLy_071012
{
    public partial class Form_Custom_Management_Add : Form
    {
        public int count = 0;
        private readonly Main _frCustomerManagement;
        public CustomerEntity cs = new CustomerEntity();
        public ContactEntity _contact = new ContactEntity();

        public Form_Custom_Management_Add(Main frCustomerManagement)
        {
            InitializeComponent();
            _frCustomerManagement = frCustomerManagement;
        }

        //Button Cancel
        private void btnCan_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Are you want to quit?", "Warning", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                this.Close();
            }
        }//End

        //CLICK "X" BUTTON
        private void Form_Custom_Management_Detail_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result1 = MessageBox.Show("Are you want to quit?", "Warning", MessageBoxButtons.YesNo);
            if (result1 != DialogResult.Yes)
                this.Close();

        }//End

        //TXT TAX
        private void txtTax_TextChanged(object sender, EventArgs e)
        {
            if (txtTax.Text == "")
            {
                lblChecktax.Visible = true;
            }
            else
            {
                lblChecktax.Visible = false;
            };

            if (checkRequire())
                btnAdd.Enabled = true;

            else
                btnAdd.Enabled = false;

            if (checkRequire())
                btnSave.Enabled = true;

            else
                btnSave.Enabled = false;


        }//End


        //TXTCODE
        private void txtCod_TextChanged(object sender, EventArgs e)
        {
            if (txtCode.Text == "")
            {
                lblCheckcode.Visible = true;
            }
            else
            {
                lblCheckcode.Visible = false;
            };

            string code = txtCode.Text;
            if (!CheckCode(code))
            {
                lblCheck.Text = "This code have existed!";

            }
            else
            {
                lblCheck.Text = "";
            }
        }//End


        //TXTCUS
        private void txtCus_TextChanged(object sender, EventArgs e)
        {
            if (txtCompany.Text == "")
            {

                lblCheckcus.Visible = true;

            }
            else
            {

                lblCheckcus.Visible = false;

            };

            if (checkRequire())
                btnAdd.Enabled = true;
            else
                btnAdd.Enabled = false;

            if (checkRequire())
                btnSave.Enabled = true;

            else
                btnSave.Enabled = false;
        }//End

        //TXTCOM
        private void txtCom_TextChanged(object sender, EventArgs e)
        {
            if (txtContact.Text == "")
            {

                lblCheckcom.Visible = true;


            }
            else
            {
                lblCheckcom.Visible = false;
            };

            if (checkRequire())
                btnAdd.Enabled = true;
            else
                btnAdd.Enabled = false;
            if (checkRequire())
                btnSave.Enabled = true;

            else
                btnSave.Enabled = false;
        }//End

        //RTBADD
        private void rtbAdd_TextChanged(object sender, EventArgs e)
        {
            if (rtbAdd.Text == "")
            {

                lblCheckadd.Visible = true;

            }
            else
            {

                lblCheckadd.Visible = false;

            };
            if (checkRequire())
                btnAdd.Enabled = true;
            else
                btnAdd.Enabled = false;

            if (checkRequire())
                btnSave.Enabled = true;

            else
                btnSave.Enabled = false;
        }//End

        //TXTTEL
        private void txtTel_TextChanged(object sender, EventArgs e)
        {
            if (txtTel.Text == "")
            {

                lblChecktel.Visible = true;

            }
            else
            {

                lblChecktel.Visible = false;

            };
            if (checkRequire())
                btnAdd.Enabled = true;
            else
                btnAdd.Enabled = false;
            if (checkRequire())
                btnSave.Enabled = true;

            else
                btnSave.Enabled = false;

        }//End

        //COMBO COUNTRY
        private void cboCry_SelectedIndexChanged(object sender, EventArgs e)
        {
            string check1 = cboCry.SelectedItem.ToString();
            if (check1 == "")
            {

                lblCheckcry.Visible = true;

            }
            else
            {

                lblCheckcry.Visible = false;

            };
            if (checkRequire())
                btnAdd.Enabled = true;
            else
                btnAdd.Enabled = false;

            if (checkRequire())
                btnSave.Enabled = true;

            else
                btnSave.Enabled = false;

            LoadProvince();

        }//End

        //COMBO CITI
        private void cboCti_SelectedIndexChanged(object sender, EventArgs e)
        {
            string check2 = cboCti.SelectedItem.ToString();
            if (check2 == "")
            {

                lblCheckcti.Visible = true;

            }
            else
            {

                lblCheckcti.Visible = false;

            };
            if (checkRequire())
                btnAdd.Enabled = true;
            else
                btnAdd.Enabled = false;
            if (checkRequire())
                btnSave.Enabled = true;

            else
                btnSave.Enabled = false;

            LoadDistrict();

        }//End

        //COMBO PROVINCE
        private void cboPce_SelectedIndexChanged(object sender, EventArgs e)
        {
            string check2 = cboPce.SelectedItem.ToString();
            if (check2 == "")
            {

                lblCheckpro.Visible = true;

            }
            else
            {

                lblCheckpro.Visible = false;

            };
            if (checkRequire())
                btnAdd.Enabled = true;
            else
                btnAdd.Enabled = false;

            if (checkRequire())
                btnSave.Enabled = true;

            else
                btnSave.Enabled = false;
        }//End

        //CHECK REQUIRE FUCTION
        private bool checkRequire()
        {
            int temp = txtTax.Text.Length * txtCompany.Text.Length * txtContact.Text.Length * rtbAdd.Text.Length * txtTel.Text.Length * cboCry.Text.Length * cboCti.Text.Length * cboPce.Text.Length;
            if (temp != 0)
                return true;

            return false;
        }



        private void Form_Custom_Management_Detail_Load(object sender, EventArgs e)
        {
            LoadSaleName();
            LoadCountry();
            LoadProvince();
            LoadDomain();
            LoadDistrict();
            if (!_frCustomerManagement._permission.AllPermission)
            {
                comboSal.Enabled = false;
                comboSal.Text = _frCustomerManagement._user.StaffName;
            }
            
        }

        /// <summary>
        /// kiem tra ton tai cua code 
        /// </summary>       
        private bool CheckCode(string code)
        {
            CustomerBO csBO = new CustomerBO();
            return csBO.CheckCode(code);
        }


        /// <summary>
        /// Load ten sale len combobox
        /// </summary>
        private void LoadSaleName()
        {
            StaffBO sBO = new StaffBO();
            comboSal.DataSource = sBO.LoadStaff();
            comboSal.DisplayMember = "StaffName";
            comboSal.Text = _frCustomerManagement._user.StaffName;
        }

        /// <summary>
        /// Load country len combobox
        /// </summary>
        private void LoadCountry()
        {
            CountryBO cBO = new CountryBO();
            cboCry.DataSource = cBO.LoadCountry();
           cboCry.DisplayMember = "CountryName";
            cboCry.SelectedIndex = 202;
        }

        /// <summary>
        /// Load province len combobox
        /// </summary>
        private void LoadProvince()
        {
            ProvinceBO pBO = new ProvinceBO();
            string CountryName = cboCry.Text;
            cboCti.DataSource = pBO.LoadProvince(CountryName);
            cboCti.DisplayMember = "Province";
        }

        /// <summary>
        /// Load district combobox
        /// </summary>
        private void LoadDistrict()
        {
            DistrictBO dBO = new DistrictBO();
            string ProvinceName = cboCti.Text;
            cboPce.DataSource = dBO.LoadDistrict(ProvinceName);
            cboPce.DisplayMember = "DistrictName";
        }

        /// <summary>
        /// Load ten sale len combobox
        /// </summary>
        private void LoadDomain()
        {
            DomainBO dBO = new DomainBO();
            cbxDomain.DataSource = dBO.LoadDomain();
            cbxDomain.DisplayMember = "DomainName";

        }

        /// <summary>
        /// Them khach hang
        /// </summary>
        public void AddCustomer()
        {
            CustomerBO csBO = new CustomerBO();
            

            cs.StaffName = comboSal.Text;
            cs.CompanyName = txtCompany.Text;
            cs.ContactPerson = txtContact.Text;
            cs.Domain = cbxDomain.Text;
            cs.CustomerCode = txtCode.Text;
            cs.TaxCode = txtTax.Text;
            cs.Country = cboCry.Text.ToString().Trim();
            cs.Province = cboCti.Text.ToString();
            cs.District = cboPce.Text.ToString();
            cs.Address = rtbAdd.Text;
            cs.Mail = txtMai.Text+"";
            cs.Web = txtMai.Text+"";
            cs.Tel = txtTel.Text+"";
            cs.Fax = txtFax.Text+"";
            cs.Remark = rtbRmk.Text+"";
            cs.ManagedFrom = DateTime.Today;
            cs.ManagedTo = DateTime.Today.AddYears(50);

           csBO.AddCustomer(cs);
            MessageBox.Show("Add successful!");

        }

        /// <summary>
        /// Them sale vao danh sach history sale
        /// </summary>
        public void AddHistorySale()
        {
            ManagedByBO mBO = new ManagedByBO();         
            ManagedByEntity info = new ManagedByEntity();

            info.StaffName=comboSal.Text;
            //info.CompanyName=txtCompany.Text;
            //info.Position=_frCustomerManagement._user.Position;
            info.Tel = _frCustomerManagement._staff.Tel;
            info.Email = _frCustomerManagement._staff.Mail;
            info.ManagedFrom = DateTime.Today;
            info.ManagedTo = DateTime.Today.AddYears(50);

            mBO.AddHistorySale(info);
        }

        public void AddText(string txt){
            txtContact.Text = txt;
        }

        public void AddContact(ContactEntity data) {
            _contact = data;
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddCustomer();
            AddHistorySale();

            ContactBO cBO = new ContactBO();
            cBO.AddContact(_contact);

            _frCustomerManagement.LoadCustomer();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void lblCheckcom_Click(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            AddCustomer();
            AddHistorySale();
            _frCustomerManagement.LoadCustomer();
            Form fr = new Form_Quotation_New_Quotation(this);
            fr.Show();
        }

        private void cbxDomain_SelectedIndexChanged(object sender, EventArgs e)
        {
            string check1 = cbxDomain.SelectedItem.ToString();
            if (check1 == "")
            {

                label2.Visible = true;

            }
            else
            {

                label2.Visible = false;

            };
            if (checkRequire())
                btnAdd.Enabled = true;
            else
                btnAdd.Enabled = false;

        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Form fr = new Form_Contact(this);
            fr.Show();            
        }

    }


}

      

      
        

    
        
     
    
